/*
 * This packet is the response from the server to inform a client that an 
 * invalid game was selected or some other error.
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;

public class InvalidGamePacket extends Packet {

    public enum ReasonCode {
        unknown_game ((short)1), 
            unsupported_game ((short)1), 
            resource_shortage ((short)2),
            server_error ((short)3);
        
        public final short value;
        ReasonCode( short value ) {
            this.value = value;
        };
    };
    
    short packet_game_name;
    short packet_game_version;
    
    ReasonCode reason_code;
    
    public InvalidGamePacket( ReasonCode reason_code,
                              short game_name, 
                              short game_version ) {
        
        /*Set up the header*/
        message_type = MSG_INVALID_GAME;
        data_area_size = 8; /*only four shorts in this packet*/
        this.game_name = 0;
        this.game_version = 0;
        
        /*Rest of the packet*/
        this.reason_code = reason_code;
        this.packet_game_name = game_name;
        this.packet_game_version = game_version;
    }
    
    public InvalidGamePacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        
        short reason_code_raw = buf.getShort();
        reason_code = ReasonCode.class.getEnumConstants()[reason_code_raw];
        
        short empty = buf.getShort();
        packet_game_name = buf.getShort();
        packet_game_version = buf.getShort();
    }
	
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        
        buf.putShort( reason_code.value );
        buf.putShort( (short)0 );
        buf.putShort( packet_game_name );
        buf.putShort( packet_game_version );
        
        return buf.array();
    }
    
    public String toString() {
        String ret = super.toString() + "\nreason_code=" + reason_code.value + " zero=" + 0;
        ret = ret + "\ngame_name=" + packet_game_name + " game_version="+packet_game_version;
        return ret;
    }
    
    public ReasonCode get_reason_code() { return reason_code; }
    public short get_packet_game_name() { return packet_game_name; }
    public short get_packet_game_version() { return packet_game_version; }
    

}
